package com.sgame.smobigame;

import java.util.ArrayList;
import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

public class FriendsView extends Activity {
	private ListView lvFriend;

	FriendAdapter adapter;
	JSONArray jaFriend = null;
	static String url = ""; // API
	static final String TAG_Friend = "friend";
	static final String TAG_NameFriend = "name";
	static final String TAG_SttFriend = "status";
	static final String TAG_AvatarFriend = "avatar";
	static final String TAG_LastPlay = "picgame";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.sgame_friends);
		// Hashmap for ListView
		ArrayList<HashMap<String, String>> friendList = new ArrayList<HashMap<String, String>>();

		// Creating JSON Parser instance
		JSONParser jParser = new JSONParser();

		// getting JSON string from URL
		JSONObject json = jParser.getJSONFromUrl(url);
		try {
			// Getting Array of Contacts
			jaFriend = json.getJSONArray(TAG_Friend);

			// looping through All Contacts
			for (int i = 0; i < jaFriend.length(); i++) {
				JSONObject c = jaFriend.getJSONObject(i);

				// Storing each json item in variable
				String stAvatarFriend = c.getString(TAG_AvatarFriend);
				String stNameFriend = c.getString(TAG_NameFriend);
				String stSttFriend = c.getString(TAG_SttFriend);
				String stLastPlay = c.getString(TAG_LastPlay);

				// creating new HashMap
				HashMap<String, String> map = new HashMap<String, String>();

				// adding each child node to HashMap key => value
				map.put(TAG_AvatarFriend, stAvatarFriend);
				map.put(TAG_NameFriend, stNameFriend);
				map.put(TAG_SttFriend, stSttFriend);
				map.put(TAG_LastPlay, stLastPlay);

				// adding HashList to ArrayList
				friendList.add(map);
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
		lvFriend = (ListView) findViewById(R.id.lvFriend);
		adapter = new FriendAdapter(this, friendList);
		lvFriend.setAdapter(adapter);

		// Click event for single list row
		lvFriend.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {

			}
		});

	}
}
